package com.yanduo.tags

import org.apache.commons.lang3.StringUtils
import org.apache.spark.broadcast.Broadcast
import org.apache.spark.sql.Row

/**
  *
  * @author Gerry chan
  * 2020/5/5 18:31
  * @version 1.0
  */
object Tags4Devices extends Tags {
  override def makeTags(args: Any*): Map[String, Int] = {

    var map = Map[String, Int]()

    //将参数转换成Row 对象
    val row = args(0).asInstanceOf[Row]
    val appdict = args(1).asInstanceOf[Broadcast[Map[String, Int]]]
    //设备
    val client = row.getAs[Int]("client")
    //获取设备联网方式字段
    val net = row.getAs[String]("networkmannername").toUpperCase
    // 获取运营商名称字段
    val opera = row.getAs[String]("ispname")

    client match {
      case 1 => map +="D00010001" -> 1
      case 2 => map +="D00010002" -> 1
      case 3 => map +="D00010003" -> 1
      case _ => map +="D00010004" -> 1
    }
    net match {
      case "WIFI" => map +="D00020001" -> 1
      case "4G" => map +="D00020002" -> 1
      case "3G" => map +="D00020003" -> 1
      case "2G" => map +="D00020004" -> 1
      case _ => map +="D00020005" -> 1
    }
    opera match {
      case "移动" => map +="D00030001" -> 1
      case "联通" => map +="D00030002" -> 1
      case "电信" => map +="D00030003" -> 1
      case _ => map +="D00030004" -> 1
    }


    val appId = row.getAs[String]("appid")
    val appName = row.getAs[String]("appname")

    if (StringUtils.isEmpty(appName)) {
      if (StringUtils.isNotEmpty(appId)) {
        //从广播变量中获取
        val newAppName = appdict.value.getOrElse(appId,appId)
        map +="APP" +newAppName
      }
    } else map+="App"+appName->1
    map
  }
}
